package com.teamc.team.controller;


import com.teamc.team.entity.Object;
import com.teamc.team.entity.User;
import com.teamc.team.response.ResponseData;
import com.teamc.team.service.ILinkService;
import com.teamc.team.service.IObjectService;
import com.teamc.team.service.IUserService;
import com.teamc.team.util.ResponseDataUtil;
import com.teamc.team.vo.UserOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ding
 * @since 2021-06-10
 */
@Controller
public class UserController {
    /**
     * 用户控制
     * */
    private final IUserService userService;

    private final ILinkService linkService;

    private final IObjectService objectService;

    public UserController(IUserService userService, ILinkService linkService, IObjectService objectService) {
        this.userService = userService;
        this.linkService = linkService;
        this.objectService = objectService;
    }

    @RequestMapping("/user/Index")
    public String x() {
        return "/user/index";
    }

    /**
     * 书籍
     * 1.有权限书籍的展示
     *
     * 订单
     * 1.无权限书籍的归还
     * 2.书籍借阅的历史记录
     *
     * 用户
     * 1.更改个人信息
     * */
    @GetMapping("/user/findAllBook/{id}")
    @ResponseBody
    public ResponseData findAllBook(@PathVariable("id") String id){
        return ResponseDataUtil.buildSuccess();
    }

    @PutMapping("/user/returnBook/{linkID}")
    @ResponseBody
    public ResponseData returnBook(@PathVariable("linkID") Integer linkId){
        try {
            objectService.returnBook(linkId);
            return ResponseDataUtil.buildSuccess("归还成功");
        } catch (Exception e) {
            return ResponseDataUtil.buildError(e);
        }
    }
    @GetMapping("/user/listAllReturned/{id}")
    @ResponseBody
    public ResponseData listAllReturned(@PathVariable("id") String id){
        try {
            List<UserOrderVO> userOrderVOS = linkService.listAllVOByReturnedForUser(id);
            return ResponseDataUtil.buildSuccess(userOrderVOS);
        } catch (Exception e) {
            return ResponseDataUtil.buildError();
        }
    }

    @GetMapping("/user/listAllNotReturned/{id}")
    @ResponseBody
    public ResponseData listAllNotReturned(@PathVariable("id") String id){
        try {
            List<UserOrderVO> userOrderVOS = linkService.listAllVOByNotReturnedForUser(id);
            return ResponseDataUtil.buildSuccess(userOrderVOS);
        } catch (Exception e) {
            return ResponseDataUtil.buildError();
        }
    }

    @GetMapping("/user/findHistory/{id}")
    @ResponseBody
    public ResponseData findAllHistory(@PathVariable("id") String id){
        try {
            List<UserOrderVO> userOrderVOS = linkService.listAllVOForUser(id);
            return ResponseDataUtil.buildSuccess(userOrderVOS);
        } catch (Exception e) {
            return ResponseDataUtil.buildError();
        }
    }

    @PutMapping("user/modifyUser")
    @ResponseBody
    public ResponseData modifyUser(User x){
        try {
            userService.modifyUser(x);
            return ResponseDataUtil.buildSuccess("修改用户信息成功！");
        } catch (Exception e) {
            return ResponseDataUtil.buildError("修改用户信息失败！");
        }
    }


//模板
//    @RequestMapping("user/search")
//    public String toSearch(){
//        return "/user/book_search_result";
//    }

    @RequestMapping("user/search")
    public String toSearch(){
        return "/user/book_search_result";
    }


    @RequestMapping("user/Record")
   public String toOwnRecord(){
        return "user/ownRecord";
   }
}
